#define _CRT_SECURE_NO_WARNINGS
#include "string.h"
#include <assert.h>
namespace gun
{
	string::string(const char* str)
	{
		_size = strlen(str);
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}
	string::string(const string& s)
	{
		string tmp(s._str);
		swap(tmp);
	}
	string& string:: operator=(string s)
	{
		swap(s);
		return *this;
	}
	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = 0;
		_capacity = 0;
	}
	const char& string::operator[](size_t pos)const
	{
		assert(pos <= _size);
		return _str[pos];
	}
	char& string::operator[](size_t)
	{
		assert(pos <= _size);
		return _str[pos];
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}
	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			size_t newCapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(newCapacity);
		}
		_str[_size] = ch;
		_size++;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(_size + len);
		}
		strcpy(_str + _size, str);
		_size += len;
	}
	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}


	void insert(size_t pos, char ch)
	{
		assert(pos <= _size);

	}
	void insert(size_t pos, char* str);
	void erase(size_t pos, size_t len = npos);
	void swap(string& s);
	size_t  find(const char ch, size_t pos = 0);
	size_t find(const char* str, size_t pos = 0);

	string substr(size_t pos = 0, size_t len = npos);
	void clear();
}